package com.cyms.modules.base.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.cyms.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cyms.modules.base.entity.CarEntity;
import com.cyms.modules.base.entity.CompanyEntity;
import com.cyms.modules.base.entity.LineEntity;
import com.cyms.modules.base.entity.LineScheduleEntity;
import com.cyms.modules.base.entity.LineTypeEntity;
import com.cyms.modules.base.service.LineScheduleService;
import com.cyms.modules.base.service.LineService;
import com.cyms.modules.base.service.LineTypeService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.cyms.common.utils.PageUtils;
import com.cyms.common.utils.R;



/**
 * 线路信息
 *
 * @author low
 * @email tolower@foxmail.com
 * @date 2018-12-05 22:19:22
 */
@RestController
@RequestMapping("base/line")
public class LineController extends AbstractController {
	@Autowired
    private LineTypeService lineTypeService;
    @Autowired
    private LineService lineService;
    @Autowired
	private LineScheduleService lineScheduleService;
    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("base:line:list")
    public R list(@RequestParam Map<String, Object> params){
		params.put("createOrg",getUser().getCompanyId());
        PageUtils page = lineService.queryPage(params);

        return R.ok().put("page", page);
    }

    @RequestMapping("/select")
    @RequiresPermissions("base:line:list")
    public R select() {
    	List<LineTypeEntity> topLineTypeList = lineTypeService.selectList(
    			new EntityWrapper<LineTypeEntity>()
						.eq("pid", 0)
						.eq("create_org",getUser().getCompanyId())
		);
    	List<Map<String,Object>> list = topLineTypeList.parallelStream().map(topLineType->{
    		Map<String,Object> map=new HashMap<>();
    		map.put("label", topLineType.getName());
    		map.put("value", topLineType.getId());
    		//
    		List<LineTypeEntity> lineTypeList=lineTypeService.selectList(new EntityWrapper<LineTypeEntity>().eq("pid", topLineType.getId()));
    		map.put("children", lineTypeList.parallelStream().map(lineType->{
    			Map<String,Object> lineMap=new HashMap<>();
    			lineMap.put("label", lineType.getName());
    			lineMap.put("value", lineType.getId());
    			//
    			List<LineEntity> lineList=lineService.selectList(new EntityWrapper<LineEntity>().eq("type_id", lineType.getId()));
    			lineMap.put("children", lineList.parallelStream().map(line->{
    				Map<String,Object> data=new HashMap<>();
    				data.put("label", line.getName());
    				data.put("value", line.getId());
    				return data;
    			}).collect(Collectors.toList()));
    			return lineMap;
    		}).collect(Collectors.toList()));
    		return map;
    	}).collect(Collectors.toList());
    	return R.ok().put("lineOptions", list);
    }
    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("base:line:info")
    public R info(@PathVariable("id") Long id){
			LineEntity line = lineService.selectById(id);
			List<LineScheduleEntity> list=lineScheduleService.selectList(new EntityWrapper<LineScheduleEntity>().eq("line_id", line.getId()));
			line.setScheduleList(list);
        return R.ok().put("line", line);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("base:line:save")
    public R save(@RequestBody LineEntity line){
    	line.setCreateDate(new Date());
    	line.setCreateUser(getUserId());
    	line.setCreateDept(getUser().getDeptId());
    	line.setCreateOrg(getUser().getCompanyId());
		lineService.insert(line);
		for (LineScheduleEntity lineScheudle : line.getScheduleList()) {
			lineScheudle.setLineId(line.getId());
			lineScheduleService.insertOrUpdate(lineScheudle);
		}
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("base:line:update")
    public R update(@RequestBody LineEntity line){
			lineService.updateById(line);
			List<LineScheduleEntity> list=lineScheduleService.selectList(new EntityWrapper<LineScheduleEntity>().eq("line_id", line.getId()));
			lineScheduleService.deleteBatchIds(list.parallelStream().map(s->s.getId()).collect(Collectors.toList()));
			for (LineScheduleEntity lineScheudle : line.getScheduleList()) {
				lineScheudle.setLineId(line.getId());
				lineScheduleService.insertOrUpdate(lineScheudle);
			}
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("base:line:delete")
    public R delete(@RequestBody Long[] ids){
			lineService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

}
